﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TS.Entities;
using TS.Commons;
using System.Data;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Globalization;

namespace TS.DataAccess
{
    /// <summary>
    /// Author: VuongPT
    /// </summary>
    public class CommandDataAccess
    {
        /// <summary>
        /// Parse object to Entity
        /// </summary>
        /// <param name="dataRecord"></param>
        /// <returns></returns>
        public static CommandObj ParseToEntity(IDataRecord dataRecord)
        {
            return new CommandObj
            {
                CommandId = dataRecord["CommandId"].ToStringOrDefault().ToNumeric(),
                OwnerId = dataRecord["OwnerId"].ToStringOrDefault().ToNumeric(),
                NumberCont = dataRecord["NumberCont"].ToStringOrDefault(),
                StartDate = dataRecord["StartDate"].ToStringOrDefault(),
                EndDate = dataRecord["EndDate"].ToStringOrDefault(),
                Save1 = dataRecord["Save1"].ToStringOrDefault(),
                Price = float.Parse(dataRecord["Price"].ToStringOrDefault()),
                Save2 = dataRecord["Save2"].ToStringOrDefault(),
                TotalPrice = float.Parse(dataRecord["TotalPrice"].ToStringOrDefault())
            };
        }

        /// <summary>
        /// Select All Command
        /// </summary>
        /// <returns></returns>
        public Collection<CommandObj> GetAllCommand()
        {
            Collection<SqlParameter> collection = new Collection<SqlParameter>();

            int totalRecord = 0;
            using (IDataReader reader = SqlHelper.ExecuteReader(UICommand.SelectAllCommandQuery, collection))
            {
                var result = new Collection<CommandObj>();
                while (reader.Read())
                {
                    CommandObj cmd = ParseToEntity(reader);
                    result.Add(cmd);
                }
                if (reader.NextResult())
                {
                    if (reader.Read())
                    {
                        totalRecord = reader["TotalRowCount"].ToNumeric();
                    }
                }
                return result;
            }
        }

        /// <summary>
        /// Insert Command
        /// </summary>
        /// <param name="cmdObj"></param>
        /// <returns></returns>
        public int InsertCommand(CommandObj cmdObj)
        {
            int result = 0;
            if (cmdObj != null)
            {
                var parameters = new Collection<SqlParameter>
                                                          {
                                                              new SqlParameter("@OwnerId",cmdObj.OwnerId),
                                                              new SqlParameter("@NumberCont",cmdObj.NumberCont),
                                                              new SqlParameter("@StartDate",cmdObj.StartDate),
                                                              new SqlParameter("@EndDate",cmdObj.EndDate),
                                                              new SqlParameter("@Save1",cmdObj.Save1),
                                                              new SqlParameter("@Price",cmdObj.Price),
                                                              new SqlParameter("@TotalPrice",cmdObj.TotalPrice)
                                                          };

                using (IDataReader reader = SqlHelper.ExecuteReader(UICommand.InsertCommandQuery, parameters))
                {
                    if (reader != null)
                    {
                        while (reader.Read())
                        {
                            result = Convert.ToInt32(reader["CommandId"], CultureInfo.CurrentCulture);
                        }
                        return result;
                    }
                }
            }

            return result;
        }
    }
}
